home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Help with gettng 2 chars into an integer!
- Date: 11 Jan 1996 20:00:08 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Jan11130008@qcd.lanl.gov>
- References: <4d2eh1$7pm@usc.edu> <4d3e2u$1hr@crl.crl.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: bobfry@crl.com's message of 11 Jan 1996 08:32:30 -0800
-
- --text follows this line--
- In article <4d3e2u$1hr@crl.crl.com> bobfry@crl.com (Robert Fry) writes:
- <snip>
- >Hi. I can't seem to figure out how to do this. Basically I have to
- chars that
- >I want to put into a 2-byte integer. It seems easy at first, but I
- can't seem
- >to figure out to do it with the bit-fidling operators (shifting or
- masking).
- >Simple put, I'd like to do:
-
- > char a,b;
- > int c;
-
- > a = 'A';
- > b = 'B';
- > c = ? /* the first byte in c should contain the value of a, and the
- >second byte should contain the value of b */
-
- Assuming chars are 8 bits, and assuming sizeof( int) >= 2 * sizeof( char),
- then you can use the following (note that it may be incorrect with
- respect to the endianness of your CPU, and is clearly not portable):
-
- c = (( a << 8) & 0x0FF00) | ( b & 0x00FF);
-
- This will work. You can also put a and b into a union that contains an
- array of 2 characters and an int. I'm not sure how to make the structure
- independent of endianness, but someone should have an idea if that's an
- issue for you.
-
- I've used both, depending on the needs of the moment.
-
- There have been a number of answers to the quetsion, I am choosing to
- follow up on this because only this post stated it assumptions
- correctly. One of the assumptions could be waekened by using CHAR_BIT
- instead of 8; whether that is meaningful depends on whether the
- original poster meant `byte' to be an arbitrary length capable of
- holding a char: as C defines it, or an octet as often common in
- networking or binary file format descriptions.
-
- The reason for this follow up is, however, to add a note about the use
- of unions for this purpose. Strictly speaking, once you store
- something into one member of an union, accessing a different member
- has implementation defined behaviour. Hence, an implementation may
- specify aggressive optimization in which it defines the behaviour to
- be undefined. It is unlikely that an implementation will choose to do
- so by default (on marketing grounds), but it is allowed. Similarly
- byte order issues (and `holes' in integral types), are, by this rule,
- to be addressed by the individual implementation; and, no portable
- solution is possible.
-
- Having said that, I must admit that most often when one needs to do
- things like this, one is probably not looking for absolute portability
- anyway. I have often used unions and structs with bitfields even for
- non-portable, but fast and readable code.
-
- Cheers
- Tanmoy
-
-
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-